// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <WinSock2.h>
#include <mysql.h>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>




void clearScreen() {
	system("cls");  // Windows
	// system("clear"); // Unix/Linux
}
char toUpperCase(char c) {
	return std::toupper(static_cast<unsigned char>(c));
}

using namespace std;
MYSQL mysql;
MYSQL_RES* res;//存储
MYSQL_ROW row;  //提取


const char DataBase_UserName[] = "root";    //数据库用户名username
const char DataBase_Password[] = "123456";   //数据库密码，填自己的密码
const char DataBase_Host[] = "localhost";  //数据库连接地址,写127.0.0.1也行
const char DataBase_Name[] = "data";  //数据库名称
unsigned int DataBase_Port = 3306;
void connect();
void UserLogin();
void UserEnroll();
void StudentOperation();
void TeacherOperation();
void MessageChange(string UserAccount);
void SearchMessage();
void CreateQue();
void PasswordChange(string UserAccount);
void sql_panduan(string sql);
void StudentDelete();
void Test();
void TestBegan();
void QueSearch();
void QueDelete();

enum choice
{
	login,
	enroll,
	logout
};
enum student_choice
{
	message_change = 1,
	text_parpare,
	student_logout

};
enum teacher_choice
{
	message_change_te = 1,
	search_message,
	s_m_d,
	create_que,
	search_que,
	account_logout


};
enum message
{
	studentID = 1,
	password
};
enum search_way
{
	all,
	student_id,
	account,
	role

};



void main()
{
	connect();
	int operation;

	while (1)
	{
		printf("请选择操作（0.登录,1.注册,2.退出）:");
		cin >> operation;
		switch (choice(operation))
		{
		case login: UserLogin();
			break;
		case enroll:UserEnroll();
			break;
		case logout:return;
		default:printf("操作有误,请重新选择");
			break;
		}
	}
	printf("结束");
}
void connect()
{
	mysql_init(&mysql);
	if (mysql_real_connect(&mysql, DataBase_Host, DataBase_UserName, DataBase_Password, DataBase_Name, DataBase_Port, NULL, 0))
	{
		printf("连接成功....\n");
		mysql_query(&mysql, "use data;");

	}
}
void UserLogin()
{
	string UserAccount, UserPasswd;;
	cout << "请输入用户账号：";
	cin >> UserAccount;
	cout << "请输入用户密码：";
	cin >> UserPasswd;
	string sql = "SELECT role FROM message where account = \"" + UserAccount + "\" and  password = \"" + UserPasswd + "\";";
	cout << "执行的sql语句为:" << sql << endl;
	const char* ss = sql.c_str();
	mysql_query(&mysql, ss);
	res = mysql_store_result(&mysql);

	if (row = mysql_fetch_row(res))
	{
		clearScreen();
		cout << "登录成功" << endl;
		string role = row[0];
		string UserAccount_now = UserAccount;
		if (role == "st")
		{
			cout << "欢迎同学" << endl;
			while (1)
			{

				StudentOperation();
				int student_operation;
				cin >> student_operation;
				switch (student_choice(student_operation))
				{

				case message_change:MessageChange(UserAccount_now);
					break;
				case text_parpare:TestBegan();
					break;
				case student_logout:return;
				default:printf("操作有误");
					break;
				}
			}
		}
		else
		{
			cout << "欢迎老师" << endl;


			while (1)
			{
				TeacherOperation();
				int teacher_operation;
				cin >> teacher_operation;
				switch (teacher_choice(teacher_operation))
				{
				case message_change_te:MessageChange(UserAccount_now);
					break;
				case search_message:SearchMessage();
					break;
				case s_m_d:StudentDelete();
					break;
				case create_que:CreateQue();
					break;
				case search_que: QueSearch();
					break;
				case account_logout:return;
				default:cout << "操作有误,请重新输入" << endl;
					break;
				}
			}

		}


	}
	else
	{
		printf("账户不存在或账号密码错误,登录失败\n");


	}

}
void UserEnroll()
{

	string cid, cname, cpwd, crol;
	cout << "请输入学号:";//将这些信息输出显示；
	cin >> cid;//键盘输入信息；
	cout << "请输入要创建的账号:";
	cin >> cname;
	cout << "请输入账号的密码:";
	cin >> cpwd;

	cout << "请输入你的身份:";
	cin >> crol;
	bool tem = false;

	if (crol == "te" || "st")
	{
		tem = true;
	}

	while (tem = false)
	{
		cout << "无此类身份,请再次输入(te/st):";
		cin >> crol;
		if (crol == "te" || "st")
		{
			break;
		}
	}
	string NewUser = "insert into message(StudentID ,account,password,role) values( \"" + cid + "\", \"" + cname + "\", \"" + cpwd + "\", \"" + crol + "\");";
	cout << NewUser << endl;
	const char* ss = NewUser.c_str();
	if (mysql_query(&mysql, ss))
	{
		printf("创建失败\n");
	}
	else
	{
		printf("创建成功\n");
	}
}
void StudentOperation()

{
	cout << "请输入以下数字来执行操作" << endl;
	cout << "1.修改信息" << endl;
	cout << "2.准备考试" << endl;
	cout << "3.退出" << endl;


}
void TeacherOperation()
{
	cout << "请输入以下数字来执行操作" << endl;
	cout << "1.修改信息" << endl;
	cout << "2.查询信息" << endl;
	cout << "3.删除学生信息" << endl;
	cout << "4.出题" << endl;
	cout << "5.查题" << endl;
	cout << "6.退出" << endl;
	cout << "请输入要执行的操作:";
}
void MessageChange(string UserAccount)
{
	cout << "请输入要修改的内容(1.学号,2.密码):";
	int choice;
	cin >> choice;
	string account, studentid;
	string sql;//sql语句
	switch (message(choice))
	{
	case studentID:cout << "请输入修改后的学号：";
		cin >> studentid;
		sql = "update message  set StudentID  = \"" + studentid + "\"   where account = \"" + UserAccount + "\" ;";
		sql_panduan(sql);
		break;
	case password:PasswordChange(UserAccount);
		break;
	default:printf("操作有误,请重新输入");
		break;
	}


}
void SearchMessage()
{
	cout << "请输入查询方式(0.查询全部,1.学号查询,2.账户查询,3.角色查询):";
	string s, ID, name, Role;//查询方式
	int search_operation;//查询操作
	cin >> search_operation;
	switch (search_way(search_operation))
	{
	case all:s = "select* from message;";
		break;
	case student_id:
		cout << "请输入学号:";
		cin >> ID;
		s = "select * from message where StudentID ='" + ID + "';";
		break;
	case account:
		cout << "请输入账户:";
		cin >> name;
		s = "select * from message where account ='" + name + "';";
		break;
	case role:
		cout << "请输入角色(st/te):";
		cin >> Role;
		s = "select * from message where role ='" + Role + "';";
		break;
	default:printf("操作有误");
		break;
	}
	cout << s << endl;
	const char* ss = s.c_str();
	if (mysql_query(&mysql, ss))
	{
		printf("查询用户失败(%s)\n", mysql_error(&mysql));
	}
	else if (mysql_affected_rows(&mysql) == 0)
	{
		printf("该用户不存在\n");
	}
	else if (mysql_affected_rows(&mysql) > 0)
	{
		printf("查询的用户存在\n");
		//printf("%s\n", ss);
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");
		res = mysql_store_result(&mysql);
		//显示数据
		//给ROW赋值，判断ROW是否为空，不为空就打印数据。
		while (row = mysql_fetch_row(res))
		{
			cout << left << "      " << setw(9) << row[0] << "|     "
				<< setw(15) << row[1] << "|     "
				<< setw(9) << row[2] << "|     "
				<< setw(9) << row[3] << "|     "
				<< setw(9) << row[4] << endl;
		}
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");
		cout << "本次查询结束" << endl;

	}

}
void CreateQue()
{
	string qid, qname, qa, qb, qc, qd, qans, qscore;

	cout << "请输入题目：" << endl;
	cin >> qname;

	string s = "select * from question where qname = \"" + qname + "\";";
	const char* ss = s.c_str();
	mysql_query(&mysql, ss);
	printf("%s\n", ss);
	res = mysql_store_result(&mysql);
	if (mysql_num_rows(res) > 0)
	{
		printf("添加的试题和表中的试题有重复的，创建项目不成功\n");
	}
	else
	{
		cout << "请输入题号：";
		cin >> qid;
		cout << "请输入选项A：";
		cin >> qa;
		cout << "请输入选项B：";
		cin >> qb;
		cout << "请输入选项C：";
		cin >> qc;
		cout << "请输入选项D：";
		cin >> qd;
		cout << "请输入标准答案：";
		cin >> qans;
		cout << "请输入分值：";
		cin >> qscore;
		s = "insert into question( qid ,qname,choice_a,choice_b,choice_c,choice_d,choice_ans,qscore) values( \"" +
			qid + "\", \"" + qname + "\", \"" + qa + "\", \"" + qb + "\",\"" + qc + "\",\"" + qd + "\",\"" + qans + "\",\"" + qscore + "\");";
		ss = s.c_str();
		printf("%s\n", ss);
		if (mysql_query(&mysql, ss))
		{
			printf("添加试题失败(%s)", mysql_error(&mysql));

		}
		if (mysql_affected_rows(&mysql) > 0)
		{
			printf("试题添加成功\n");
		}


	}




}
void PasswordChange(string UserAccount)
{
	string UserPassword1, UserPassword2;
	cout << "请输入修改后的用户密码：";
	cin >> UserPassword1;
	cout << "请确认密码：";
	cin >> UserPassword2;
	while (UserPassword2 != UserPassword1)
	{
		cout << "两次密码输入不相同，请重新输入";
		cout << "请输入修改后的用户密码：" << endl;
		cin >> UserPassword1;
		cout << "请确认密码：" << endl;
		cin >> UserPassword2;
	}
	string sql = "update message  set password  = \"" + UserPassword1 + "\"   where account = \"" + UserAccount + "\" ;";
	cout << "执行的sql语句为:" << sql << endl;
	const char* ss = sql.c_str();

	if (!mysql_query(&mysql, ss))
	{
		cout << "修改成功" << endl;
	}
	else
	{
		cout << "修改失败" << endl;
	}
}
void StudentDelete()
{
	cout << "请输入删除方式(1.学号查询,2.账户查询,3.取消删除):";
	string s, ID, name, s1;//查询方式
	int search_operation;//查询操作
	cin >> search_operation;
	switch (search_way(search_operation))
	{
	case student_id:
		cout << "请输入学号:";
		cin >> ID;
		s = "DELETE FROM message WHERE   StudentID  ='" + ID + "' and role='te' ;";
		s1 = "select * from message where StudentID ='" + ID + "' and role='te' ;";
		break;
	case account:
		cout << "请输入账户:";
		cin >> name;
		s = "DELETE FROM message WHERE account='" + name + "' and role='te' ;";
		s1 = "select * from message where account ='" + name + "' and role='te' ;";
		break;
	case 3:return;
	default:printf("操作有误");
		break;
	}
	const char* ss = s1.c_str();
	if (!mysql_query(&mysql, ss))
	{

		printf("查询的用户存在\n");
		//printf("%s\n", ss);
		cout << ss << endl;
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");
		res = mysql_store_result(&mysql);
		string role;
		while (row = mysql_fetch_row(res))
		{
			cout << left << "      " << setw(9) << row[0] << "|     "
				<< setw(15) <<row[1] << "|     "
				<< setw(9) << row[2] << "|     "
				<< setw(9) << row[3] << "|     "
				<< setw(9) << row[4] << endl;
				role = row[4];
		}
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");

		string sure;
		cout << role << endl;
		cout << "确认删除(Y/N)?:";
		ss = s.c_str();
		cin >> sure;
		cout << s << endl;
		if (sure == "Y")
		{
			if (role =="st")
			{
			  cout << "删除成功" << endl;
			}
			else
			{
				cout << "你只能删除学生" << endl;
			}
			
			
		}

		else if (sure == "N")

		{
			cout << "删除取消" << endl;
		}
		else
		{
			cout << "删除失败" << endl;
		}
	}
	else
	{
		cout << "查无此人" << endl;
	}

}
void Test()
{
	string sure;
	cout << "Are you ready(Y/N)：";
	cin >> sure;
	if (sure == "Y")
	{
		string sql = "SELECT * FROM question ORDER BY RAND() LIMIT 3;";

		mysql_query(&mysql, sql.c_str());
		//获取结果集
		res = mysql_store_result(&mysql);
		//显示数据
		//给ROW赋值，判断ROW是否为空，不为空就打印数据。
		int i = 1;
		while (row = mysql_fetch_row(res))
		{
			cout << left << setw(9) << i
				<< setw(35) << row[1] << "A:"
				<< setw(9) << row[2] << "B:"
				<< setw(9) << row[3] << "C:"
				<< setw(9) << row[4] << "D:"
				<< setw(9) << row[5] << "答案:"
				<< setw(9) << row[6] << "分值:"
				<< setw(9) << row[7] << endl;
			i++;
			cout << endl;
		}
	}
	else
	{
		cout << "考试已取消" << endl;
		return;
	}


}
void TestBegan()
{
	const int QuNumber = 3;
	int Score = 0, Number = 0;
	string sql = "SELECT * FROM question ORDER BY RAND() LIMIT 3;";
	mysql_query(&mysql, sql.c_str());
	//获取结果集
	res = mysql_store_result(&mysql);
	int i = 1;
	char stu_choice;
	char StuChoice[QuNumber];
	char AnswerChoice[QuNumber];
	int score[QuNumber];
	while (row = mysql_fetch_row(res))
	{
		cout << left << i << ". "
			<< setw(25) << row[1] << "A:"
			<< setw(9) << row[2] << "B:"
			<< setw(9) << row[3] << "C:"
			<< setw(9) << row[4] << "D:"
			<< setw(9) << row[5] << endl << "请选择(A,B,C,D):";
		//int answer= row[6]
		cin >> stu_choice;

		StuChoice[i - 1] = toUpperCase(stu_choice);
		AnswerChoice[i - 1] = row[6][0];
		score[i - 1] = stoi(row[7]);
		i++;
	}
	for (int k = 0; k < i - 1; k++)
	{

		cout << StuChoice[k] << " " << AnswerChoice[k] << endl;
		if (StuChoice[k] == AnswerChoice[k])
		{
			Score += score[k];
			Number++;
		}

	}

	printf("学生共答对了%d题目,得分为%d\n", Number, Score);
	if (Score < 60)
	{
		printf("菜鸡一个,哈哈哈哈哈哈\n");
		printf("\n");
	}
	else if (Score == 100)
	{
		cout << "牛逼" << endl;
		printf("\n");
	}
	{

	}

}
void QueSearch()
{
	string sql = "SELECT * FROM question ;";
	mysql_query(&mysql, sql.c_str());
	//获取结果集
	res = mysql_store_result(&mysql);

	for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
	{
		printf("-");
	}printf("\n");
	//显示数据
	//给ROW赋值，判断ROW是否为空，不为空就打印数据。
	while (row = mysql_fetch_row(res))
	{
		cout << left << "      " << setw(9) << row[0] << "|     "
			<< setw(35) << row[1] << "|     "
			<< setw(9) << row[2] << "|     "
			<< setw(9) << row[3] << "|     "
			<< setw(9) << row[4] << "|     "
			<< setw(9) << row[5] << "|     "
			<< setw(9) << row[6] << "|     "
			<< setw(9) << row[7] << endl;
	}
	for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
	{
		printf("-");
	}printf("\n");
	cout << "请选择操作(1.删除/2.取消):";
	int operation;
	cin >> operation;
	switch (operation)
	{
	case 1:QueDelete();
		break;
	case 2:return;
		break;
	default:
		break;
	}



}
void QueDelete()
{
	cout <<"请输入要删除的题目的序号:";
	string id;
	cin >>id;
	string sql="select * from question where qid  = " + id + ";";
	string sql1 = "DELETE FROM question WHERE  qid  = " + id +";";
	cout<< sql<<endl;
	const char* ss = sql.c_str();
	if (!mysql_query(&mysql, ss))
	{

		printf("查询的问题存在\n");
		for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
		{
			printf("-");
		}printf("\n");

		res = mysql_store_result(&mysql);
		//显示数据
		//给ROW赋值，判断ROW是否为空，不为空就打印数据。
		while (row = mysql_fetch_row(res))
		{
			cout << left << "      " << setw(9) << row[0] << "|     "
				<< setw(35) <<row[1] << "|     "
				<< setw(9) << row[2] << "|     "
				<< setw(9) << row[3] << "|     "
				<< setw(9) << row[4] << "|     "
				<< setw(9) << row[5] << "|     "
				<< setw(9) << row[6] << "|     "
				<< setw(9) << row[7] << endl;
		}
		for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
		{
			printf("-");
		}printf("\n");
		string sure;
		cout << "确认删除(Y/N)?:";
		ss = sql1.c_str();
		cin >> sure;
		if (sure == "Y")
		{
			if (!mysql_query(&mysql, ss))
			{
			 cout << "删除成功" << endl;
			}
			else
			{
				cout << "出现问题,删除失败" << endl;
			}
		}

		else if (sure == "N")

		{
			cout << "删除取消" << endl;
		}
		
	}
	else
	{
		cout << "查无此问题" << endl;
	}

	



}

void sql_panduan(string sql)
{

	const char* ss = sql.c_str();

	if (!mysql_query(&mysql, ss))
	{
		cout << "修改成功" << endl;
	}
	else
	{
		cout << "修改失败" << endl;
	}
}


